<?php

namespace app\common\service\order\pipeline\coupon;

use api\channel\jutuike\AroundUnion;
use app\common\service\around\JtkAroundService;
use app\model\CouponOrder;
use think\Exception;

/**
 * 聚推客要求的 效验订单规则
 */
class JutuikeAroundValidate
{

    public static function handle(CouponOrder $couponOrder, $next) {
        $params = $couponOrder->params;
        $serv_type = $params['serv_type'] ?? '';

        if ($serv_type != PRODUCT_AROUND_SHOP_SERV) {
            return $next($couponOrder);
        }

        // 聚推客需要的参数
        $jutuikeParams = [
            'thirdPartyOrderNo' => $couponOrder->order_id,
            'productId' => $params['productId'],
            'itemId' => $params['itemId'],
            'locationId' => $params['locationId'],
            'customerName' => $params['customerName'],
            'customerPhoneNumber' => $params['customerPhoneNumber'],
            'quantity' => $params['quantity'],
        ];
        $couponOrder->params['jutuikeCheckOrderParams'] = $jutuikeParams; // 聚推客效验请求的参数

        self::checkRules($couponOrder);

        self::checkOrder($couponOrder);


        $next($couponOrder);
    }

    /**
     * 效验规则
     * @param CouponOrder $couponOrder
     * @return void
     */
    public static function checkRules(CouponOrder &$couponOrder) {
        $jutuikeParams = $couponOrder->params['jutuikeCheckOrderParams'];

        $rules = [
            'thirdPartyOrderNo' => 'require',
            'productId' => 'require',
            'itemId' => 'require',
            'locationId' => 'require',
            'quantity' => 'require|regex:^[1-9]\d*$',
            'customerName' => 'require',
            'customerPhoneNumber' => 'require|mobile',
        ];

        $message = [
            'thirdPartyOrderNo.require' => '订单编号不能为空',
            'productId.require' => '产品编号id不能为空',
            'itemId.require' => '套餐编号id不能为空',
            'locationId.require' => '站点编号不能为空',
            'quantity.require' => '购买数量不能为空',
            'quantity.regex' => '购买数量格式有误',
            'customerName.require' => '客户姓名不能为空',
            'customerPhoneNumber.require' => '客户电话不能为空',
            'customerPhoneNumber.mobile' => '客户电话格式有误',
        ];
        $msg = dmjValidate($rules, $message, $jutuikeParams);

        if ($msg) {
            throw new Exception($msg);
        }
    }

    /**
     * 聚推客 效验订单
     * @param CouponOrder $couponOrder
     * @return void
     */
    public static function checkOrder(CouponOrder &$couponOrder) {
        $jutuikeParams = $couponOrder->params['jutuikeCheckOrderParams'];
        $aroundUnion = new AroundUnion();
        $res = $aroundUnion->checkCreateOrder($jutuikeParams);
        $code = $res['code'] ?? JtkAroundService::CODE_FAIL;
        if ($code != JtkAroundService::CODE_SUCCESS) {
            $msg = $res['msg'] ?? '购买商品订单效验异常，请联系管理员';
            throw new Exception($msg);
        }
        $dataList = $res['data'] ?? [];
        $couponOrder->params['checkCreateOrderJtkRes'] = $dataList;
    }

}